/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import olympicgameslogic.*;
import olympicgameslogic.Discipline.DISCIPLINETYPE;

/**
 *
 * @author fabio mendonca
 */
public class DialogManageDisciplines extends JFrame {

   private JTextField txtdis = new JTextField(15);
   private JComboBox comboTipo1 = new JComboBox(Discipline.DISCIPLINETYPE.values());
   private JComboBox comboTipo2 = new JComboBox(ClassificationType.MEASUREUNIT.values());
   private JComboBox comboTipo3 = new JComboBox(ClassificationType.ORDER.values());
   private ImageIcon img;
   private String tit;
   ClassLoader cldr = this.getClass().getClassLoader();
   private Discipline dis;
   private DialogManageModalities da;
   private Modality m;

   public DialogManageDisciplines(String titulo, Discipline dis, DialogManageModalities da, Modality m) {
      super(titulo);
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      this.dis = dis;
      this.da = da;
      this.m = m;
   }

   public void showJan() {
      if (dis == null) {
	 img = new ImageIcon(cldr.getResource("images/extra/page_add.png"));
	 tit = "Add Discipline";
      } else {
	 img = new ImageIcon(cldr.getResource("images/extra/page_edit.png"));
	 tit = "Edit Discipline";
      }
      setLayout(new BorderLayout());
      setBackground(Color.white);

//    Criação da barra de topo
      JPanel pannorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
      pannorte.setSize(200, 60);
      pannorte.setBackground(Color.white);
      JLabel background = new JLabel(tit);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);
      pannorte.add(background);

// criação dos campos             
      JPanel pancentro = new JPanel();
      pancentro.setBackground(Color.white);
      pancentro.setLayout(new GridLayout(2, 1));

//campo Nome e Tipo        
      JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      JLabel d1 = new JLabel("Discipline:");
      p1.add(d1);
      p1.add(txtdis);
      p1.setBackground(Color.white);
      JLabel d2 = new JLabel("Type:");
      p1.add(d2);
      comboTipo1.setSelectedIndex(-1);
      p1.add(comboTipo1);
      pancentro.add(p1);

// tipo de utilizador    Male/female    
      JPanel p0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      JLabel d3 = new JLabel("Measure Type:");
      p0.add(d3);
      comboTipo2.setSelectedIndex(-1);
      p0.add(comboTipo2);
      JLabel d4 = new JLabel("Order:");
      p0.add(d4);
      comboTipo3.setSelectedIndex(-1);
      p0.add(comboTipo3);
      p0.setBackground(Color.white);
      pancentro.add(p0);

      if (dis == null) {
	 comboTipo1.setSelectedItem(0);
	 comboTipo2.setSelectedItem(0);
	 comboTipo3.setSelectedItem(0);
	 txtdis.setText("");
      } else {
	 comboTipo1.setSelectedItem(dis.getType());
	 comboTipo2.setSelectedItem(dis.getcType().getUnit());
	 comboTipo3.setSelectedItem(dis.getcType().getOrder());
	 txtdis.setText(dis.getName());
      }

//painel com botoes        
      JPanel pansul = new JPanel();
      pansul.setSize(200, 20);
      pansul.setBackground(Color.white);
      JButton btok = new JButton("OK", new ImageIcon(cldr.getResource("images/extra/btok.png")));
      pansul.setLayout(new FlowLayout(FlowLayout.CENTER));
      pansul.add(btok);


      add(pannorte, BorderLayout.NORTH);
      add(pancentro, BorderLayout.CENTER);
      add(pansul, BorderLayout.SOUTH);
      setSize(400, 180);
      setResizable(false);
      setLocationRelativeTo(null);
      setVisible(true);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);



      btok.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (txtdis.getText().equalsIgnoreCase("")) {
	       JOptionPane.showMessageDialog(DialogManageDisciplines.this, "Please set Discipline name", getTitle(), JOptionPane.WARNING_MESSAGE);
	       txtdis.requestFocus();
	    } else if (comboTipo1.getSelectedIndex() < 0 || comboTipo2.getSelectedIndex() < 0 || comboTipo2.getSelectedIndex() < 0) {
	       JOptionPane.showMessageDialog(DialogManageDisciplines.this, "You must select all options", getTitle(), JOptionPane.WARNING_MESSAGE);
	    } else {

	       if (dis == null) {
		  ClassificationType c = new ClassificationType(ClassificationType.cTypeUnits[comboTipo2.getSelectedIndex()], ClassificationType.cTypeOrder[comboTipo3.getSelectedIndex()]);
		  Discipline.DISCIPLINETYPE g = Discipline.types[comboTipo1.getSelectedIndex()];

		  Discipline d = new Discipline(txtdis.getText().trim(), m, c, g);
		  m.getDisciplines().add(d);


	       } else {
		  dis.setName(txtdis.getText());
		  dis.setType(Discipline.types[comboTipo1.getSelectedIndex()]);
		  ClassificationType c = new ClassificationType(ClassificationType.cTypeUnits[comboTipo2.getSelectedIndex()], ClassificationType.cTypeOrder[comboTipo3.getSelectedIndex()]);
		  dis.setcType(c);
	       }

	       da.refreshTable();
	       dispose();
	    }
	 }
      });
   }
}
